#pragma once

#include<iostream>
using namespace std;

namespace Key
{
	template<class K>
	struct BSTNode
	{
		BSTNode* _left;
		BSTNode* _right;
		K _key;

		BSTNode(const K& key = K())
			:_left(nullptr)
			, _right(nullptr)
			, _key(key)
		{}
	};

	template<class K>
	class BSTree
	{
	public:
		typedef BSTNode<K> Node;
		using Node = BSTNode<K>;

		BSTree() = default;

		BSTree(const BSTree& t)
		{
			_root = _Copy(t._root);
		}

		BSTree& operator=(BSTree t)
		{
			swap(_root, t._root);

			return *this;
		}

		~BSTree()
		{
			_Destory(_root);
		}

		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}

			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}

			Node* newnode = new Node(key);
			if (parent->_key < key)
			{
				parent->_right = newnode;
			}
			else
			{
				parent->_left = newnode;
			}
			return true;
		}

		void Insert2(const K& key)
		{
			_root = _Insert(_root, key);
		}

		bool Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key) cur = cur->_right;
				else if (cur->_key > key) cur = cur->_left;
				else return true;
			}
			return false;
		}

		bool Find2(const K& key)
		{
			return _Find(_root, key);
		}

		bool Erase(const K& key)
		{
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}
						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}
						delete cur;
					}
					else
					{
						Node* replaceParent = cur;
						Node* replace = cur->_right;
						while (replace->_left)
						{
							replaceParent = replace;
							replace = replace->_left;
						}
						cur->_key = replace->_key;

						if (replaceParent->_left == replace)
							replaceParent->_left = replace->_right;
						else 
							replaceParent->_right = replace->_right;

						delete replace;
					}
					return true;
				}
			}
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

	private:
		Node* _root = nullptr;

		Node* _Copy(Node* root)
		{
			if (root == nullptr) return nullptr;

			Node* newnode = new Node(root->_key);
			newnode->_left = _Copy(root->_left);
			newnode->_right = _Copy(root->_right);

			return newnode;
		}

		void _Destory(Node* root)
		{
			if (root == nullptr) return;

			_Destory(root->_left);
			_Destory(root->_right);

			delete root;
		}

		Node* _Insert(Node* root, const K& key)
		{
			if (root == nullptr) return new Node(key);

			if (root->_key < key)
				root->_right = _Insert(root->_right, key);
			else if(root->_key > key)
				root->_left = _Insert(root->_left, key);

			return root;
		}

		bool _Find(Node* root, const K& key)
		{
			if (root == nullptr) return false;

			bool ret1 = false;
			bool ret2 = false;

			if (root->_key < key)
				ret1 = _Find(root->_right, key);
			else if (root->_key > key)
				ret2 = _Find(root->_left, key);
			else return true;

			return ret1 || ret2;
		}

		void _InOrder(Node* root)
		{
			if (root == nullptr) return;

			_InOrder(root->_left);
			cout << root->_key << " ";
			_InOrder(root->_right);
		}
	};
}